<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html>
<head>
<title>Toolbars in Java Gnome</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="GUI, Java Gnome, toolbars, Java, programming, linux">
<meta name="description" content="Toolbars in Java Gnome">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div> 

<div class="content">


<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>Toolbars in Java Gnome</h1>

<p>
In this part of the Java Gnome programming tutorial, we will work with toolbars.
</p>

<div class="center">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-9706709751191532";
/* top_horizontal */
google_ad_slot = "3327173442";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<p>
Menus group commands that we can use in application. 
Toolbars provide a quick access to the most frequently used commands. 
</p>


<h2>Simple toolbar</h2>

<p>
Next we create a simple toolbar. 
</p>

<div class="codehead">toolbar.java</div> 
<pre class="code">
package com.zetcode;

import org.gnome.gdk.Event;
import org.gnome.gtk.Gtk;
import org.gnome.gtk.Orientation;
import org.gnome.gtk.SeparatorToolItem;
import org.gnome.gtk.Stock;
import org.gnome.gtk.ToolButton;
import org.gnome.gtk.Toolbar;
import org.gnome.gtk.VBox;
import org.gnome.gtk.Widget;
import org.gnome.gtk.Window;
import org.gnome.gtk.WindowPosition;


/**
 * ZetCode Java Gnome tutorial
 *
 * This program creates a simple toolbar.
 *
 * @author jan bodnar
 * website zetcode.com
 * last modified March 2009
 */


public class GToolbar extends Window {

    public GToolbar() {
        setTitle("Toolbar");
        
        initUI();
        
        connect(new Window.DeleteEvent() {
            public boolean onDeleteEvent(Widget source, Event event) {
                Gtk.mainQuit();
                return false;
            }
        });
        
        setDefaultSize(300, 250);
        setPosition(WindowPosition.CENTER);
        showAll();
    }
    
    
    public void initUI() {

        Toolbar toolbar = new Toolbar();

        ToolButton newtb = new ToolButton(Stock.NEW);
        ToolButton opentb = new ToolButton(Stock.OPEN);
        ToolButton savetb = new ToolButton(Stock.SAVE);
        SeparatorToolItem sep = new SeparatorToolItem();
        ToolButton quittb = new ToolButton(Stock.QUIT);

        toolbar.insert(newtb, 0);
        toolbar.insert(opentb, 1);
        toolbar.insert(savetb, 2);
        toolbar.insert(sep, 3);
        toolbar.insert(quittb, 4);
       
        quittb.connect(new ToolButton.Clicked() {

            public void onClicked(ToolButton toolButton) {
                Gtk.mainQuit();
            }
        });
         
        VBox vbox = new VBox(false, 2);
        vbox.packStart(toolbar, false, false, 0);
        
        add(vbox);
    }
    
    
    public static void main(String[] args) {
        Gtk.init(args);
        new GToolbar();
        Gtk.main();
    }
}
</pre>

<p>
The example shows a toolbar and four tool buttons. 
</p>

<pre class="explanation">
 Toolbar toolbar = new Toolbar();
</pre>

<p>
A <b class="keyword">Toolbar</b> widget is created. 
</p>


<pre class="explanation">
 ToolButton newtb = new ToolButton(Stock.NEW);
</pre>

<p>
A <b class="keyword">ToolButton</b> with an image from
stock is created. 
</p>

<pre class="explanation">
 SeparatorToolItem sep = new SeparatorToolItem();
</pre>

<p>
This is a separator. It can be used to separate toolbar buttons
into logical groups. 
</p>

<pre class="explanation">
 toolbar.insert(newtb, 0);
 toolbar.insert(opentb, 1);
 ...
</pre>

<p>
Toolbar buttons are inserted into the toolbar widget. 
</p>


<br>
<img src="/img/gui/javagnome/toolbar.png" alt="Toolbar">
<div class="figure">Figure: Toolbar</div>
<br>


<h2>Toolbars</h2>

<p>
In the second example, we show two toolbars. Many applications 
have more than one toolbar. We show, how we can do it in Java Gnome. 
</p>

<div class="codehead">toolbars.java</div> 
<pre class="code">
package com.zetcode;

import org.gnome.gdk.Event;
import org.gnome.gtk.Gtk;
import org.gnome.gtk.Stock;
import org.gnome.gtk.ToolButton;
import org.gnome.gtk.Toolbar;
import org.gnome.gtk.VBox;
import org.gnome.gtk.Widget;
import org.gnome.gtk.Window;
import org.gnome.gtk.WindowPosition;


/**
 * ZetCode Java Gnome tutorial
 *
 * This program creates two toolbar
 * instances.
 *
 * @author jan bodnar
 * website zetcode.com
 * last modified March 2009
 */


public class GToolbars extends Window {

    public GToolbars() {
        setTitle("Toolbars");
        
        initUI();
        
        connect(new Window.DeleteEvent() {
            public boolean onDeleteEvent(Widget source, Event event) {
                Gtk.mainQuit();
                return false;
            }
        });
        
        setDefaultSize(300, 250);
        setPosition(WindowPosition.CENTER);
        showAll();    
    }
    
    
    public void initUI() {
        
        Toolbar upper = new Toolbar();

        ToolButton newtb = new ToolButton(Stock.NEW);
        ToolButton opentb = new ToolButton(Stock.OPEN);
        ToolButton savetb = new ToolButton(Stock.SAVE);

        upper.insert(newtb, 0);
        upper.insert(opentb, 1);
        upper.insert(savetb, 2);

        Toolbar lower = new Toolbar();

        ToolButton quittb = new ToolButton(Stock.QUIT);
   
        quittb.connect(new ToolButton.Clicked() {

            public void onClicked(ToolButton toolButton) {
                Gtk.mainQuit();
            }
        });
        
        lower.insert(quittb, 0);

        VBox vbox = new VBox(false, 2);
        vbox.packStart(upper, false, false, 0);
        vbox.packStart(lower, false, false, 0);

        add(vbox);
    }
    
    
    public static void main(String[] args) {
        Gtk.init(args);
        new GToolbars();
        Gtk.main();
    }
}
</pre>

<p>
The application shows two toolbars. 
</p>

<pre class="explanation">
 Toolbar upper = new Toolbar();
 ...
 Toolbar lower = new Toolbar();
</pre>

<p>
We create two <b class="keyword">Toolbar</b> widgets. 
</p>

<pre class="explanation">
 upper.insert(newtb, 0);
 ...       
 lower.insert(quittb, 0);
</pre>

<p>
Each of them has it's own tool buttons. 
</p>

<pre class="explanation">
 VBox vbox = new VBox(false, 2);
 vbox.packStart(upper, false, false, 0);
 vbox.packStart(lower, false, false, 0);
</pre>

<p>
Toolbars are packed into the vertical box, one after the other. 
</p>


<br>
<img src="/img/gui/javagnome/toolbars.png" alt="Toolbars">
<div class="figure">Figure: Toolbars</div>
<br>

<h2>Undo redo</h2>

<p>
The following example demonstrates, how we can inactivate toolbar buttons on the toolbar.
It is a common practise in GUI programming. For example the save button.
If we save all changes of our document to the disk, the save button is inactivated 
in most text editors. This way the application indicates to the user, 
that all changes are already saved. 
</p>

<div class="codehead">undoredo.java</div>
<pre class="code">
package com.zetcode;

import org.gnome.gdk.Event;
import org.gnome.gtk.Gtk;
import org.gnome.gtk.Label;
import org.gnome.gtk.SeparatorToolItem;
import org.gnome.gtk.Stock;
import org.gnome.gtk.ToolButton;
import org.gnome.gtk.Toolbar;
import org.gnome.gtk.VBox;
import org.gnome.gtk.Widget;
import org.gnome.gtk.Window;
import org.gnome.gtk.WindowPosition;

/**
 * ZetCode Java Gnome tutorial
 *
 * This program creates an undo redo 
 * example. It shows how to enable/disable
 * tool buttons, if they cannot be used no
 * more.
 *
 * @author jan bodnar
 * website zetcode.com
 * last modified March 2009
 */

public class GUndoRedo extends Window {

    private int count = 2;
    private ToolButton undo;
    private ToolButton redo;

    public GUndoRedo() {
    
        setTitle("Undo redo");
        
        initUI();
        
        connect(new Window.DeleteEvent() {
            public boolean onDeleteEvent(Widget source, Event event) {
                Gtk.mainQuit();
                return false;
            }
        });
        
        setDefaultSize(300, 250);
        setPosition(WindowPosition.CENTER);
        showAll();    
    }
    
    public void initUI() {
        Toolbar toolbar = new Toolbar();
        
        undo = new ToolButton(Stock.UNDO);
        redo = new ToolButton(Stock.REDO);
        SeparatorToolItem sep = new SeparatorToolItem();
        ToolButton quit = new ToolButton(Stock.QUIT);

        toolbar.insert(undo, 0);
        toolbar.insert(redo, 1);
        toolbar.insert(sep, 2);
        toolbar.insert(quit, 3);

        undo.connect(new ToolButton.Clicked() {

            public void onClicked(ToolButton toolButton) {
                count -= 1;

                if (count <= 0) {
                    undo.setSensitive(false);
                    redo.setSensitive(true);
                }
            }
        });
        
        redo.connect(new ToolButton.Clicked() {

            public void onClicked(ToolButton toolButton) {
                count += 1;

                if (count >= 5) {
                    redo.setSensitive(false);
                    undo.setSensitive(true);
                }
            }
        });
        
        quit.connect(new ToolButton.Clicked() {

            public void onClicked(ToolButton toolButton) {
                Gtk.mainQuit();
            }
        });
         
        VBox vbox = new VBox(false, 2);
        vbox.packStart(toolbar, false, false, 0);
        vbox.packStart(new Label(""), false, false, 0);

        add(vbox);

    }
    
    public static void main(String[] args) {
        Gtk.init(args);
        new GUndoRedo();
        Gtk.main();
    }
}
</pre>

<p>
Our example creates undo and redo buttons from the Java Gnome stock resources. 
After several clicks each of the buttons is inactivated. The buttons are grayed out.  
</p>

<pre class="explanation">
 private int count = 2;
</pre>

<p>
The <b class="keyword">count</b> variable decides, which button is activated and
deactivated. 
</p>


<pre class="explanation">
 undo = new ToolButton(Stock.UNDO);
 redo = new ToolButton(Stock.REDO);
</pre>

<p>
We have two tool buttons. Undo and redo tool buttons. Images come from the stock
resources. 
</p>

<pre class="explanation">
 undo.connect(new ToolButton.Clicked() {
     public void onClicked(ToolButton toolButton) {
          count -= 1;
          if (count <= 0) {
              undo.setSensitive(false);
              redo.setSensitive(true);
          }
      }
  });
</pre>

<p>
We plug a method for the <b class="keyword">Clicked</b> event for the
undo tool button. To activate/deactivate a widget, we use the
<b class="keyword">setSensitive()</b> method. 
</p>


<br>
<img src="/img/gui/javagnome/undoredo.png" alt="Undo redo">
<div class="figure">Figure: Undo redo</div>
<br>

<hr class="btm">

<p>
In this chapter of the Java Gnome programming library, we mentioned toolbars. 
</p>


<br>
<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div> 
<br>


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡ 
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified March 12, 2009  <span class="copyright">&copy; 2007 - 2012 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>
